package com.dream.dao.base;

/**
 * 
 * @author Evan
 * @date 2013-8-25
 * 
 */
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.dream.model.app.AppCodeItem;
import com.dream.utils.hibernate.DetachedCriteriaBuilder;
import com.dream.utils.hibernate.DreamBaseDAO;
import com.dream.utils.hibernate.PagingVO;
import com.dream.vo.app.AppCodeVO;

@Repository
public class AppCodeItemDAO extends DreamBaseDAO<AppCodeItem> {
	
	private DetachedCriteriaBuilder initQueryBuilder(final AppCodeVO vo){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(AppCodeItem.class);
		builder.innerJoin("appCode", "ac").addEq("ac.name", vo.getAppCodeName());
		builder.addEq("deleted", false).addEq("id", vo.getId());
		builder.addEq("appCodeId", vo.getAppCodeId()).addLikeAny("name", vo.getName());
		
		return builder;
	}
	
	public List<AppCodeItem> queryByAppCodeName(String appCodeName){
		
		AppCodeVO vo = new AppCodeVO();vo.setAppCodeName(appCodeName);
		DetachedCriteriaBuilder builder = initQueryBuilder(vo);
		builder.setOrder("sequence");
		return this.select(builder);
	}
	
	public PagingVO query(final AppCodeVO vo,final PagingVO page){
		
		if(StringUtils.isBlank(page.getOrderName())){
			page.setOrderName("sequence");
		}
		DetachedCriteriaBuilder queryBuilder = initQueryBuilder(vo);
		DetachedCriteriaBuilder countBuilder = initQueryBuilder(vo);
		return this.selectPagingVO(queryBuilder, page, countBuilder);
		
	}
	
	public boolean existed(final AppCodeVO vo){
		
		DetachedCriteriaBuilder builder = initQueryBuilder(vo);
		return CollectionUtils.isNotEmpty(select(builder));
		
	}
	
}
